<?php
declare(strict_types=1);

namespace SixShop\Message\Model;

use SixShop\Message\Entity\MessageSettingEntity;

/**
 * 消息设置模型
 */
class MessageSettingModel
{
    /**
     * 获取消息设置列表
     *
     * @param array $params 查询参数
     * @param int $page 页码
     * @param int $limit 每页数量
     * @return array
     */
    public function getList(array $params = [], int $page = 1, int $limit = 10)
    {
        $query = MessageSettingEntity::where('1=1');
        
        // 用户ID筛选
        if (isset($params['user_id']) && $params['user_id'] !== '') {
            $query->where('user_id', $params['user_id']);
        }
        
        // 获取总数
        $total = $query->count();
        
        // 获取分页数据
        $list = $query->order('id', 'desc')
            ->page($page, $limit)
            ->select()
            ->toArray();
        
        return [
            'list' => $list,
            'total' => $total,
            'page' => $page,
            'limit' => $limit,
        ];
    }
    
    /**
     * 获取系统默认设置
     *
     * @return array|null
     */
    public function getDefaultSetting()
    {
        return MessageSettingEntity::where('user_id', 0)->find();
    }
    
    /**
     * 获取用户消息设置
     *
     * @param int $userId 用户ID
     * @return array|null
     */
    public function getUserSetting(int $userId)
    {
        if ($userId <= 0) {
            return null;
        }
        
        $setting = MessageSettingEntity::where('user_id', $userId)->find();
        if (!$setting) {
            // 如果用户没有设置，则使用系统默认设置
            $defaultSetting = $this->getDefaultSetting();
            if ($defaultSetting) {
                $setting = new MessageSettingEntity();
                $setting->user_id = $userId;
                $setting->system_notify = $defaultSetting->system_notify;
                $setting->activity_notify = $defaultSetting->activity_notify;
                $setting->order_notify = $defaultSetting->order_notify;
                $setting->logistics_notify = $defaultSetting->logistics_notify;
                $setting->private_message = $defaultSetting->private_message;
                $setting->save();
            }
        }
        
        return $setting;
    }
    
    /**
     * 更新或创建消息设置
     *
     * @param int $userId 用户ID
     * @param array $data 设置数据
     * @return bool
     */
    public function updateSetting(int $userId, array $data)
    {
        $setting = MessageSettingEntity::where('user_id', $userId)->find();
        if (!$setting) {
            $setting = new MessageSettingEntity();
            $setting->user_id = $userId;
        }
        
        // 更新设置
        if (isset($data['system_notify'])) {
            $setting->system_notify = (int)$data['system_notify'];
        }
        if (isset($data['activity_notify'])) {
            $setting->activity_notify = (int)$data['activity_notify'];
        }
        if (isset($data['order_notify'])) {
            $setting->order_notify = (int)$data['order_notify'];
        }
        if (isset($data['logistics_notify'])) {
            $setting->logistics_notify = (int)$data['logistics_notify'];
        }
        if (isset($data['private_message'])) {
            $setting->private_message = (int)$data['private_message'];
        }
        
        return $setting->save();
    }
    
    /**
     * 更新系统默认设置
     *
     * @param array $data 设置数据
     * @return bool
     */
    public function updateDefaultSetting(array $data)
    {
        return $this->updateSetting(0, $data);
    }
    
    /**
     * 检查指定类型的通知是否对用户开启
     *
     * @param int $userId 用户ID
     * @param int $type 通知类型：1-系统通知，2-活动通知，3-订单通知，4-物流通知，5-私信
     * @return bool
     */
    public function isNotifyEnabled(int $userId, int $type)
    {
        if ($userId <= 0) {
            return true;
        }
        
        $setting = $this->getUserSetting($userId);
        if (!$setting) {
            return true;
        }
        
        return $setting->isNotifyEnabled($type);
    }
}
